<template>
  <el-dialog
    :visible="isShow"
    :modal="false"
    title="添加项目信息"
    @close="closeDialog"
  >
    <el-form ref="form" :model="project" label-width="80px">
      <el-form-item
        label="项目名称"
        prop="projectName"
        :rules="[{ required: true, message: '项目名称不能为空' }]"
      >
        <el-input
          class="input-box"
          v-model="project.projectName"
          placeholder="请输入项目名称"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="项目编码"
        prop="projectId"
        :rules="[{ required: true, message: '项目编码不能为空' }]"
      >
        <el-input
          class="input-box"
          v-model="project.projectId"
          placeholder="请输入项目编码"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目位置" prop="location">
        <el-input
          class="input-box"
          v-model="project.location"
          placeholder="请输入项目位置信息"
          size="small"
        ></el-input>
      </el-form-item>
      <div class="btns">
        <el-button type="plain" size="small" @click="closeDialog"
          >取消</el-button
        >
        <el-button type="primary" size="small" @click="createProject"
          >添加</el-button
        >
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddProject',
  props: {
    isShow: {
      type: Boolean,
      default: () => Boolean
    }
  },
  data () {
    return {
      project: {
        projectName: '',
        projectId: '',
        location: ''
      }
    }
  },

  methods: {
    closeDialog () {
      this.$emit('closeDialog', 'showAddProject')
    },

    // 提交项目信息
    createProject () {
      this.$refs.form.validate(async (a, b) => {
        if (a) {
          const options = this.project
          const res = await this.$store.dispatch('createProject', options)
          this.closeDialog()
          if (res.success) {
            this.$message({
              type: 'success',
              message: '新项目创建成功',
              showClose: true
            })
          } else {
            this.$message({
              type: 'info',
              message: '新项目创建失败,请重新添加',
              showClose: true
            })
          }
        }
      })
    }
  }
}
</script>

<style>
.el-form {
  margin: 0 auto;
  width: 400px;
}
.input-box {
  width: 300px;
}
</style>
